﻿@model NetSurveillance.WebApp.DataServices.Models.IpSetting

@{
    ViewBag.Title = string.Format(Resources.MenuResource.SetupWizard_ServerIpSetting, ViewBag.Step ?? string.Empty);
    Layout = "~/Views/Shared/_SetupWizardLayout.cshtml";
}
@section css{
    <style type="text/css">
        fieldset {
            width: 80%;
            margin: 10px auto;
        }
    </style>
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <fieldset>
        <legend>@ViewBag.Title</legend>
        @Html.Partial("../System/_IpSetting", Model)
    </fieldset>
    <div class="buttonsContain controlBottom">
        <div class="rightContain">
            <input id="btnPreSubmit" type="button" value="@Resources.Resource.Button_PreStep"/>
            <input id="btnSubmit" type="submit" value="@Resources.Resource.Button_NextStep"/>
        </div>
        <div class="ui-helper-clearfix"></div>
    </div>
    <input type="hidden" id="hiPreSubmit" name="preSubmit"/>
}
@section javascript
{
    <script type="text/javascript">
        $(function() {
            $('#btnSubmit').click(checkSvrIp);
            $('#btnPreSubmit').click(function () {
                if ($('form').valid()) {
                    $.showLoading('正在设置IP，请等待');
                    sendChk(function() {
                        $('#hiPreSubmit').val('PreStep');
                        $('form').trigger('submit');
                    });
                    return false;
                }
                $('#hiPreSubmit').val('PreStep');
                $('form')[0].submit();
            });
        });

        function checkSvrIp() {
            if (!$('form').valid()) return false;
            $.showLoading('正在设置IP，请等待');
            sendChk(function () {
                $('form').trigger('submit');
            });
            return false;
        }

        function sendChk(succCb) {
            var ipSettingObj = $('form').serializeObject();
            $.ajax({
                url: '@Url.Action("CheckSvrIpSetting", "System")',
                cache: false,
                type: 'post',
                dataType: 'json',
                data: ipSettingObj,
                success: function (result) {
                    if (result.success && !result.expandos) {
                        setTimeout(function() {
                            sendChk(succCb);
                        }, 3000);
                        return;
                    }
                    if (result.success && result.expandos && result.expandos.isValid)
                        succCb && typeof (succCb) == 'function' && succCb.call();
                    if (!result.success) {
                        alert(result.message);
                        $.hideLoading();
                    }
                }
            });
        }
    </script>
}
